<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('workorder.orders.user_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.user_id"
                        prop="user_id"
                        :input-attr="{ pk: 'user.id', field: 'nickname', 'remote-url': '/admin/user.User/index' }"
                        :placeholder="t('Please select field', { field: t('workorder.orders.user_id') })"
                    />
                    <FormItem
                        :label="t('workorder.orders.title')"
                        type="string"
                        v-model="baTable.form.items!.title"
                        prop="title"
                        :placeholder="t('Please input field', { field: t('workorder.orders.title') })"
                    />
                    <FormItem
                        v-if="baTable.table.extend!.fieldConfig.urgentRank"
                        :label="t('workorder.orders.urgent_rank_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.urgent_rank_id"
                        prop="urgent_rank_id"
                        :input-attr="{ pk: 'urgentrank.id', field: 'name', 'remote-url': '/admin/workorder.Urgentrank/index' }"
                        :placeholder="t('Please select field', { field: t('workorder.orders.urgent_rank_id') })"
                    />
                    <FormItem
                        v-if="baTable.table.extend!.fieldConfig.category"
                        :label="t('workorder.orders.category_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.category_id"
                        prop="category_id"
                        :input-attr="{ pk: 'clue.id', field: 'title', 'remote-url': '/admin/Clue/index' }"
                        :placeholder="t('Please select field', { field: t('workorder.orders.category_id') })"
                    />
                    <FormItem
                        :label="t('workorder.orders.engineers_ids')"
                        type="remoteSelects"
                        v-model="baTable.form.items!.engineers_ids"
                        prop="engineers_ids"
                        :input-attr="{ pk: 'engineers.id', field: 'title', 'remote-url': '/admin/workorder.Engineers/index' }"
                        :placeholder="t('Please select field', { field: t('workorder.orders.engineers_ids') })"
                    />
                    <!-- 动态字段-s -->
                    <div class="dynamic-fields" v-for="(item, idx) in baTable.table.extend!.fieldConfig.fields" :key="idx">
                        <FormItem
                            v-if="item.type == 'number'"
                            :label="item.title"
                            :type="item.type"
                            v-model.number="baTable.form.items![item.name]"
                            :attr="{
                                prop: item.name,
                                blockHelp: item.tip_msg,
                                ...item.formitem_extend,
                            }"
                            :input-attr="{ ...item.input_extend }"
                            :key="'number-' + item.id"
                        />
                        <FormItem
                            v-else-if="item.type == 'editor'"
                            :label="item.title"
                            :type="item.type"
                            @keyup.enter.stop=""
                            @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            v-model="baTable.form.items![item.name]"
                            :attr="{
                                prop: item.name,
                                blockHelp: item.tip_msg,
                                ...item.formitem_extend,
                            }"
                            :input-attr="{
                                style: {
                                    zIndex: 99,
                                },
                                ...item.input_extend,
                            }"
                            :key="'editor-' + item.id"
                        />
                        <FormItem
                            v-else-if="item.type == 'textarea'"
                            :label="item.title"
                            :type="item.type"
                            @keyup.enter.stop=""
                            @keyup.ctrl.enter="baTable.onSubmit(formRef)"
                            v-model="baTable.form.items![item.name]"
                            :attr="{
                                prop: item.name,
                                blockHelp: item.tip_msg,
                                ...item.formitem_extend,
                            }"
                            :input-attr="{ rows: 3, ...item.input_extend }"
                            :key="'textarea-' + item.id"
                        />
                        <FormItem
                            v-else
                            :label="item.title"
                            :type="item.type"
                            v-model="baTable.form.items![item.name]"
                            :attr="{
                                prop: item.name,
                                blockHelp: item.tip_msg,
                                ...item.formitem_extend,
                            }"
                            :input-attr="{ ...item.input_extend }"
                            :data="{ content: item.content ? item.content : {} }"
                            :key="'other-' + item.id"
                        />
                    </div>
                    <!-- 动态字段-e -->
                    <FormItem
                        :label="t('workorder.orders.status')"
                        type="radio"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :data="{
                            content: {
                                pending: t('workorder.orders.status pending'),
                                assigned: t('workorder.orders.status assigned'),
                                dealing: t('workorder.orders.status dealing'),
                                feedback: t('workorder.orders.status feedback'),
                                evaluate: t('workorder.orders.status evaluate'),
                                done: t('workorder.orders.status done'),
                            },
                        }"
                        :placeholder="t('Please select field', { field: t('workorder.orders.status') })"
                    />
                    <FormItem
                        v-if="baTable.form.operate == 'Edit' && baTable.table.extend!.fieldConfig.userUrging"
                        :label="t('workorder.orders.last_urging_time')"
                        type="datetime"
                        v-model="baTable.form.items!.last_urging_time"
                        prop="last_urging_time"
                        :placeholder="t('Please select field', { field: t('workorder.orders.last_urging_time') })"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    last_urging_time: [buildValidatorData({ name: 'date', title: t('workorder.orders.last_urging_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('workorder.orders.update_time') })],
    create_time: [buildValidatorData({ name: 'date', title: t('workorder.orders.create_time') })],
    ...baTable.form.extend!.rules,
})
</script>

<style scoped lang="scss"></style>
